﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Zhuang.Utility.WinForm.Event;
using Zhuang.Model.Access;
using Zhuang.Utility.Common.Validation;
using Zhuang.DAL.Access;

namespace Zhuang.WinForm.Sales
{
    public partial class Pay : Form
    {
        MaxNumberDAL maxNumberDAL = new MaxNumberDAL();
        Pos_SalesOrdersMasterDAL pos_SalesOrdersMasterDAL = new Pos_SalesOrdersMasterDAL();


        PayEntity payEnity;

        public Pay()
        {
            InitializeComponent();
        }

        public Pay(PayEntity payEnity)
        {
            InitializeComponent();
            this.payEnity = payEnity;
        }

        private void btnCheckOut_Click(object sender, EventArgs e)
        {
            Pos_SalesOrdersMasterEntity some = new Pos_SalesOrdersMasterEntity();

            some.SalesOrderMasterId= maxNumberDAL.GetMaxNumber("POS");
            some.Amount = Convert.ToDecimal(tbPayable.Text.Trim());
            some.Quantity = 0;
            some.Cash = Convert.ToDecimal(tbCash.Text.Trim());
            some.BankCard = Convert.ToDecimal(tbBankCard.Text.Trim());
            some.Change = Convert.ToDecimal(tbChange.Text.Trim());
            some.SalesOrderDate = DateTime.Now;

            try {

                MessageBox.Show(pos_SalesOrdersMasterDAL.Add(some));

                this.DialogResult = DialogResult.OK;
                this.Close();

            }catch
            {
            
            }
            
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void Cancel()
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void ShortcutKeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
            {
                Cancel();
            }
            else if (e.KeyCode == Keys.Enter)
            {

            }
            else if (e.KeyCode == Keys.F1)
            {
                tbCash.Focus();
            }
            else if (e.KeyCode == Keys.F2)
            {
                tbBankCard.Focus();
            }
        }

        private void Pay_KeyDown(object sender, KeyEventArgs e)
        {
            ShortcutKeyDown(sender, e);
        }

        private void Pay_Load(object sender, EventArgs e)
        {
            tbPayable.Text = payEnity.Payable;
        }

        private void tbCash_KeyDown(object sender, KeyEventArgs e)
        {
            ShortcutKeyDown(sender, e);
        }

        private void tbBankCard_KeyDown(object sender, KeyEventArgs e)
        {
            ShortcutKeyDown(sender, e);
        }

        private void Pay_Shown(object sender, EventArgs e)
        {
            ParseEmptyToZero();
            tbCash.Focus();
            tbCash.SelectAll();
            
        }

        private void tbPayable_KeyDown(object sender, KeyEventArgs e)
        {
            ShortcutKeyDown(sender, e);
        }

        private void tbChange_KeyDown(object sender, KeyEventArgs e)
        {
            ShortcutKeyDown(sender, e);
        }

        private void tbCash_KeyPress(object sender, KeyPressEventArgs e)
        {
            ControlsEventHelper.KeyPress_2DecimalPointInputOnly(sender,e);
        }

        private void tbBankCard_KeyPress(object sender, KeyPressEventArgs e)
        {
            ControlsEventHelper.KeyPress_2DecimalPointInputOnly(sender, e);
        }

        private void tbCash_TextChanged(object sender, EventArgs e)
        {

            CalculateChange();

        }

        private void CalculateChange()
        {
            

            decimal change = 0;
            
            decimal payable = Convert.ToDecimal(tbPayable.Text.Trim());


            decimal cash = 0;
            if (ValidateUtil.IsCanConvertDecimal(tbCash.Text.Trim()))
            {
                cash = Convert.ToDecimal(tbCash.Text.Trim());
            }
            
            decimal bankcard = 0;
            if (ValidateUtil.IsCanConvertDecimal(tbBankCard.Text.Trim()))
            {
                bankcard = Convert.ToDecimal(tbBankCard.Text.Trim());
            }
            change = (cash + bankcard)-payable;

            tbChange.Text = change.ToString("N2");
        }

        private void ParseEmptyToZero()
        {
            if (!ValidateUtil.IsCanConvertDecimal(tbCash.Text.Trim()))
            {
                tbCash.Text = "0.00";
            }

            if (!ValidateUtil.IsCanConvertDecimal(tbBankCard.Text.Trim()))
            {
                tbBankCard.Text = "0.00";
            }

            
        }

        private void tbBankCard_TextChanged(object sender, EventArgs e)
        {
            CalculateChange();
        }

        private void tbCash_Leave(object sender, EventArgs e)
        {
            ParseEmptyToZero();
        }

        private void tbBankCard_Leave(object sender, EventArgs e)
        {
            ParseEmptyToZero();
        }
    }
}
